using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace LFramework 
{
    public class UIManager : IDisposable
    {
        public Dictionary<UIGroupType, Transform> CanvasDic { get; private set; }
        //ui界面控制器列表,这个是 界面正在打开的 列表，用来刷层级使用
        private List<UICtrlBase> m_UICtrlOpeningList = new List<UICtrlBase>();
        //实例化过的 控制器脚本，防止每次new
        private Dictionary<int, UICtrlBase> m_UICtrlDic = new Dictionary<int, UICtrlBase>();

        #region 初始化

        public void Init(UIGroup[] uiGroups,float defaultWidth, float defaultHeight)
        {
            int groupCount = uiGroups.Length;

            CanvasDic = new Dictionary<UIGroupType, Transform>(groupCount);

            for (int i = 0; i < groupCount; i++) 
            {
                UIGroup uiGroup= uiGroups[i];
                CanvasScaler canvasScaler = uiGroup.GroupCanvas.GetComponent<CanvasScaler>();

                InitAdaptation(canvasScaler, defaultWidth, defaultHeight);
                CanvasDic[uiGroup.GroupType] = uiGroup.GroupCanvas;
            }
        }

        /// <summary>
        /// 通用适配
        /// </summary>
        private void InitAdaptation(CanvasScaler canvasScaler, float defaultWidth, float defaultHeight)
        {
            //0.5622
            float defaultValue = defaultWidth/ defaultHeight;
            float currValue = Screen.width * 1.0f / Screen.height;

            if (currValue < defaultValue)
                canvasScaler.matchWidthOrHeight = 1;
            else
                canvasScaler.matchWidthOrHeight = 0;
        }

        #endregion

        #region 根据UICtrl 打开 UIWindow
        public void OpenWindow<T>()  where T : UICtrlBase, new()
        {
            int key = typeof(T).GetHashCode();

            UICtrlBase ctrlValue = GetUICtrl<T>();
            if (ctrlValue == null) 
            {
                ctrlValue = new T();
                m_UICtrlDic[key] = ctrlValue;
            }

            ctrlValue.Open();
        }

        public void CloseWindow<T>() where T : UICtrlBase, new()
        {
            UICtrlBase ctrlValue = GetUICtrl<T>();
            if (ctrlValue != null)
                ctrlValue.Close();
            else
                GameEntry.LogWarning("关闭的界面不存在 : " + typeof(T).Name);
        }
        #endregion

        public void AddUI(UICtrlBase uiCtrl)
        {
            if (m_UICtrlOpeningList.IndexOf(uiCtrl) >= 0) return;
            _OpenOneUI(uiCtrl);
        }

        public void RemoveUI(UICtrlBase uiCtrl)
        {
            m_UICtrlOpeningList.Remove(uiCtrl);
        }

        public void _OpenOneUI(UICtrlBase uiCtrl)
        {
            m_UICtrlOpeningList.Add(uiCtrl);

            uiCtrl.DoOpen();
        }

        public UICtrlBase GetUICtrl<T>()
        {
            int key = typeof(T).GetHashCode();

            UICtrlBase ctrlValue = null;
            m_UICtrlDic.TryGetValue(key, out ctrlValue);
            return ctrlValue;
        }

        /// <summary>
        /// 刷新Canvas order
        /// </summary>
        public void RefreshCanvasOrder()
        {
            for (int i = 0; i < m_UICtrlOpeningList.Count; ++i)
            {
                //间隔10
                m_UICtrlOpeningList[i].WindowOrder = (i + 1) * 10;
            }
        }

        /// <summary>
        /// 释放接口
        /// </summary>
        public void Dispose()
        {
            CanvasDic.Clear();
            CanvasDic= null;
        }
    }
}

